# INSTALATION:
# /usr/local/bin:					coid, coidgen, coidc
# /usr/local/lib:					libcoidserver.so
# /usr/local/lib/coid/device:		devices


PREFIX = /usr/local
BINDIR = $(PREFIX)/bin
LIBDIR = $(PREFIX)/lib/coid
DEVDIR = $(PREFIX)/lib/coid/dev
CONFDIR = $(PREFIX)/etc/coid
MANDIR = $(PREFIX)/man/man1
INCDIR = $(PREFIX)/include

DIR__CHMOD=755
FILE_CHMOD=644

#FIXME
CPPFLAGS += " -fno-strict-aliasing"

#source bin
BINL=../bin

IS_DEBUG = $(shell test -f ".debug" && echo 1)
# first target:
core: coidlauncher coidgen

all: report core devices

report:
	@echo IS_DEBUG = $(IS_DEBUG)

devices: httpserv dev_webtalk dev_websole


.PHONY: comm coidlauncher coidgen httpserv dev_webtalk dev_websole

comm:
	@make IS_DEBUG=$(IS_DEBUG) -eC coid/comm

coidlauncher:
	@make IS_DEBUG=$(IS_DEBUG) -eC coid/server
	@make IS_DEBUG=$(IS_DEBUG) -eC coidlauncher

coidgen:
	@make IS_DEBUG=$(IS_DEBUG) -eC coidgen

httpserv:
	@make IS_DEBUG=$(IS_DEBUG) -eC device/httpserv

dev_webtalk:
	@make IS_DEBUG=$(IS_DEBUG) -eC device/webtalk

dev_websole:
	@make IS_DEBUG=$(IS_DEBUG) -eC device/websole


clean:
	@make -C coid/comm clean
	@make -C coid/server clean
	@make -C coidlauncher clean
	@make -C coidgen clean
	@make -C device/httpserv clean
	@make -C device/webtalk clean
	@make -C device/websole clean
	@find -name .depend -exec rm {} ';'





######################## INSTALATION:
install_all: install install_dev_webtalk install_dev_websole install_httpserv
	@echo Done


install_httpserv:
	@mkdir -p $(DEVDIR)/httpserv
	@cp $(BINL)/device/httpserv/* $(DEVDIR)/httpserv
	@chmod $(DIR__CHMOD) $(DEVDIR)/httpserv
	@chmod $(FILE_CHMOD) $(DEVDIR)/httpserv/*

install_dev_webtalk:
	@mkdir -p $(DEVDIR)/webtalk
	@cp $(BINL)/device/webtalk/* $(DEVDIR)/webtalk
	@chmod $(DIR__CHMOD) $(DEVDIR)/webtalk
	@chmod $(FILE_CHMOD) $(DEVDIR)/webtalk/*

install_dev_websole:
	@mkdir -p $(DEVDIR)/websole
	@cp $(BINL)/device/websole/* $(DEVDIR)/websole
	@chmod $(DIR__CHMOD) $(DEVDIR)/websole
	@chmod $(FILE_CHMOD) $(DEVDIR)/websole/*

gen_coid_config:
	@echo creating coid-config
	@mkdir -p $(BINDIR)
	@./gen-coid-config -b$(BINDIR) -l$(LIBDIR) -d$(DEVDIR) -c$(CONFDIR) -i$(INCDIR) -m$(MANDIR) > $(BINDIR)/coid-config
	@chmod a+rx $(BINDIR)/coid-config

install_headers:
	@echo installing development headers
	$(shell cd coid; find . -type f -name '*.h' | grep -v '.svn' | cpio -pd $(INCDIR)/coid )
	$(shell find $(INCDIR)/coid -type d -execdir chmod $(DIR__CHMOD) {} \; )
	$(shell find $(INCDIR)/coid -type f -execdir chmod $(FILE_CHMOD) {} \; )

install: gen_coid_config install_headers
	@echo installing config ...
	@mkdir -p $(CONFDIR)
	@cp $(BINL)/dev.conf.install $(CONFDIR)/dev.conf

	@echo installing binaries ...
	@mkdir -p $(BINDIR)
	@cp $(BINL)/coid $(BINL)/coidgen $(BINDIR)
	@chmod $(DIR__CHMOD) $(BINDIR)/coid $(BINDIR)/coidgen

	@echo installing libraries and devices ...
	@mkdir -p $(LIBDIR)/dev
	@chmod -R a+rwx $(LIBDIR)
	@cp $(BINL)/libcoidserver.so $(LIBDIR)
	@cp coid/comm/comm.a $(LIBDIR)
	
#	@echo installing man pages ...
#	@cp $(MANDIR)/coid.1 $(MANDIR)/coidgen.1 $(MANDIR)

	@chmod $(DIR__CHMOD) $(CONFDIR) $(LIBDIR)
	@chmod $(FILE_CHMOD) $(LIBDIR)/libcoidserver.so
	@chmod $(FILE_CHMOD) $(CONFDIR)/dev.conf #$(MANDIR)/coidgen.1 $(MANDIR)/coid.1

	@echo Ok

COID_CONFIG != which coid-config

uninstall_headers:
	@echo "deleting headers"
	@rm -Rf $(shell coid-config -i)/coid

uninstall: uninstall_headers
	@echo "uninstalling..."
	@echo "deleting manpages"
	@echo $(shell $(COID_CONFIG) -b)
	@rm -f $(shell coid-config -m)/{coid.1,coidgen.1}
	@echo "deleting device files"
	@rm -Rf $(shell coid-config -d)/{httpserv,websole,webtalk}
	@echo "deleting library"
	@rm -f $(shell coid-config -l)/libcoidserver.so
	@echo "deleting configuration file"
	@rm -f $(shell coid-config -c)/dev.conf
	@echo "deleting binaries: coid, coidgen, coid-config (installation paths are lost past this point)"
	@rm -f $(shell coid-config -b)/coid*
	@echo "directories have not been deleted, please remove them manually."

setdbg:
	@touch .debug
#	@touch coidlauncher/.debug
#	@touch coidgen/.debug
#	@touch coid/comm/.debug
#	@touch coid/server/.debug
#	@touch device/httpserv/.debug
#	@touch device/webtalk/.debug
#	@touch device/websole/.debug


clrdbg:
	@rm -f .debug
#	@find . -name '.debug' -exec rm {} ';'



build:
	@VER=$$(cat VERSION); \
	echo Build version: $$VER; \
	cd ..; \
	tar -jcf coid/coid-$$VER.src.tar.bz2 -X coid/.build_exclude coid

